<template>
  <view class="service-select-container">
    <!-- 标题栏 -->
    <view class="service-header">服务选择</view>

    <!-- 服务项列表 -->
    <view class="service-list">
      <view v-for="(item, idx) in services" :key="item.id" class="service-item">
        <checkbox :checked="selectedIds.includes(item.id)" @click="toggleSelect(item.id)" />
        <view class="service-info">
          <text class="service-name">{{ item.name }}</text>
          <text class="service-price">￥{{ item.price }}</text>
        </view>
      </view>
    </view>

    <!-- 底部合计和按钮 -->
    <view class="footer-bar">
      <text class="total-label">合计：</text>
      <text class="total-price">￥{{ totalPrice }}</text>
      <button class="order-btn" :disabled="selectedIds.length === 0" @click="generateOrder">生成订单</button>
    </view>

    <!-- 订单信息弹窗 -->
    <uni-popup ref="popup" type="center">
      <view class="order-popup">
        <view class="popup-title">订单信息</view>
        <view v-for="item in selectedServices" :key="item.id" class="popup-row">
          <text>{{ item.name }}</text>
          <text>￥{{ item.price }}</text>
        </view>
        <view class="popup-total">总价：￥{{ totalPrice }}</view>
        <button class="confirm-btn" @click="confirmOrder">确认</button>
      </view>
    </uni-popup>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
// 如需使用 uni-popup 组件，请确保已安装并引入

// 示例服务项数据
const services = ref([
  { id: 1, name: '发动机维修', price: 300 },
  { id: 2, name: '变速箱维修', price: 500 },
  { id: 3, name: '刹车系统维修', price: 200 },
  { id: 4, name: '空调系统维修', price: 150 },
  { id: 5, name: '轮胎更换', price: 100 }
])
const selectedIds = ref([])
const popup = ref(null)

const selectedServices = computed(() => {
  return services.value.filter(item => selectedIds.value.includes(item.id))
})
const totalPrice = computed(() => {
  return selectedServices.value.reduce((sum, item) => sum + item.price, 0)
})

function toggleSelect(id) {
  const idx = selectedIds.value.indexOf(id)
  if (idx > -1) {
    selectedIds.value.splice(idx, 1)
  } else {
    selectedIds.value.push(id)
  }
}

function generateOrder() {
  popup.value.open()
}
function confirmOrder() {
  popup.value.close()
  uni.showToast({ title: '订单已生成', icon: 'success' })
  setTimeout(() => {
    uni.reLaunch({ url: '/pages/index/index' })
  }, 800)
}
</script>

<style scoped>
.service-select-container {
  background: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 40rpx;
}
.service-header {
  background: #fff;
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
  padding: 30rpx 0 20rpx 0;
  border-bottom: 1rpx solid #eee;
  color: #333;
}
.service-list {
  margin: 20rpx;
  background: #fff;
  border-radius: 16rpx;
  padding: 10rpx 0;
}
.service-item {
  display: flex;
  align-items: center;
  padding: 18rpx 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}
.service-item:last-child {
  border-bottom: none;
}
.service-info {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-left: 20rpx;
}
.service-name {
  font-size: 30rpx;
  color: #222;
}
.service-price {
  font-size: 28rpx;
  color: #007aff;
}
.footer-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20rpx 30rpx;
  box-shadow: 0 -2rpx 8rpx rgba(0,0,0,0.04);
  z-index: 10;
}
.total-label {
  font-size: 28rpx;
  color: #333;
}
.total-price {
  font-size: 32rpx;
  color: #ff3b30;
  font-weight: bold;
  margin: 0 30rpx 0 10rpx;
}
.order-btn {
  background: #007aff;
  color: #fff;
  font-size: 30rpx;
  border-radius: 10rpx;
  padding: 12rpx 40rpx;
}
.order-btn:disabled {
  background: #ccc;
  color: #fff;
}
.order-popup {
  background: #fff;
  border-radius: 16rpx;
  padding: 40rpx 30rpx 30rpx 30rpx;
  min-width: 400rpx;
  min-height: 200rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.popup-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
}
.popup-row {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 28rpx;
  margin-bottom: 10rpx;
}
.popup-total {
  font-size: 30rpx;
  color: #ff3b30;
  font-weight: bold;
  margin: 20rpx 0 10rpx 0;
}
.confirm-btn {
  width: 100%;
  background: #2979ff;
  color: #fff;
  font-size: 30rpx;
  border-radius: 10rpx;
  padding: 16rpx 0;
}
</style> 